import json
from pathlib import Path


class AppConfig:

    FILED_OPEN_FILE_DIR = 'OPEN_FILE_DIR'
    FILED_CONFIG_FILE = 'CONFIG_FILE'

    def __init__(self) -> None:
        
        self.OPEN_FILE_DIR = Path('./')
        self.CONFIG_FILE = Path('appConfig.json')

    def load_conifg(self):
        if self.CONFIG_FILE.exists() == False:
            return
        try:
            with self.CONFIG_FILE.open('r') as f:
                data = json.load(f)
                self.OPEN_FILE_DIR = Path(data[self.FILED_OPEN_FILE_DIR])
                self.CONFIG_FILE = Path(data[self.FILED_CONFIG_FILE])
        except:
            pass
        if self.OPEN_FILE_DIR.exists() == False:
            self.OPEN_FILE_DIR = Path('.')
    
    def save_config(self):
        
        data = {}
        data.setdefault(self.FILED_CONFIG_FILE, str(self.CONFIG_FILE))
        data.setdefault(self.FILED_OPEN_FILE_DIR, str(self.OPEN_FILE_DIR))

        with self.CONFIG_FILE.open('w') as f:
            json.dump(data, f, indent=4)



SharedAppConfig = AppConfig()
SharedAppConfig.load_conifg()